//
//  ChangeNumView.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/18.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "ChangeNumView.h"
#import "SendCodeButton.h"

@interface ChangeNumView()
// 存值
@property (nonatomic, copy) NSString *phoneNumStr_old;
@property (nonatomic, copy) NSString *codeStr;
@property (nonatomic, copy) NSString *phoneNumStr_new;
@end


@implementation ChangeNumView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self loadChangeNumSubviews];
    }
    return self;
}


// loadSubviews
- (void)loadChangeNumSubviews{
    
    // 电话
    UIView *phoneNumView = [[UIView alloc]init];
    phoneNumView.backgroundColor = [UIColor whiteColor];
    [self addSubview:phoneNumView];
    [phoneNumView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(53*HEIGHT);
    }];
    
    UIImageView *phoneImgView = [[UIImageView alloc]init];
    phoneImgView.image = [UIImage imageNamed:@"login_icon_shouji"];
    phoneImgView.contentMode = UIViewContentModeScaleAspectFit;
    [phoneNumView addSubview:phoneImgView];
    [phoneImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumView).offset(23*HEIGHT);
        make.left.equalTo(phoneNumView).offset(25*WIDTH);
    }];
    
    UITextField *phoneTF = [[UITextField alloc]init];
    phoneTF.placeholder = @"请输入当前绑定的手机号";
    phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    phoneTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    phoneTF.textColor = KLColor_B2;
    phoneTF.textAlignment = NSTextAlignmentLeft;
    [phoneNumView addSubview:phoneTF];
    [[phoneTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(UITextField *phone) {
        self.phoneNumStr_old = phone.text;
    }];
    [phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgView);
        make.width.mas_equalTo(200*WIDTH);
        make.left.equalTo(phoneImgView.mas_right).offset(18*WIDTH);
    }];
    
    
    
    UIView *bottomLine_phone = [[UIView alloc]init];
    bottomLine_phone.backgroundColor = KLColor_B3;
    [phoneNumView addSubview:bottomLine_phone];
    [bottomLine_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(phoneNumView);
        make.width.mas_equalTo(325*WIDTH);
        make.height.mas_equalTo(1);
    }];
    
    
    // 新手机
    UIView *phoneNumView_new = [[UIView alloc]init];
    phoneNumView_new.backgroundColor = [UIColor whiteColor];
    [self addSubview:phoneNumView_new];
    [phoneNumView_new mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(60*HEIGHT);
    }];
    
    UIImageView *newPhoneImgView = [[UIImageView alloc]init];
    newPhoneImgView.image = [UIImage imageNamed:@"login_icon_shouji"];
    newPhoneImgView.contentMode = UIViewContentModeScaleAspectFit;
    [phoneNumView_new addSubview:newPhoneImgView];
    [newPhoneImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumView_new).offset(30*HEIGHT);
        make.left.equalTo(phoneNumView_new).offset(25*WIDTH);
    }];
    
    UITextField *newPhoneTF = [[UITextField alloc]init];
    newPhoneTF.placeholder = @"请输入新手机号";
    newPhoneTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    newPhoneTF.textColor = KLColor_B2;
    newPhoneTF.keyboardType = UIKeyboardTypeNumberPad;
    newPhoneTF.textAlignment = NSTextAlignmentLeft;
    [phoneNumView_new addSubview:newPhoneTF];
    [[newPhoneTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(UITextField *code) {
        self.phoneNumStr_new = code.text;
    }];
    [newPhoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(newPhoneImgView);
        make.width.mas_equalTo(200*WIDTH);
        make.left.equalTo(newPhoneImgView.mas_right).offset(18*WIDTH);
    }];
    
    
    UIView *lineView_phone = [[UIView alloc]init];
    lineView_phone.backgroundColor = KLColor_B3;
    [phoneNumView_new addSubview:lineView_phone];
    [lineView_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(newPhoneImgView);
        make.right.equalTo(phoneNumView_new).offset(-111*WIDTH);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(15*HEIGHT);
    }];
    
    
    SendCodeButton *codeBtn = [SendCodeButton buttonWithType:UIButtonTypeCustom];
    [codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [codeBtn setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    codeBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
    [phoneNumView_new addSubview:codeBtn];
    [codeBtn initialTime];
    [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(newPhoneImgView);
        make.centerX.equalTo(phoneNumView_new.mas_right).offset(-60*WIDTH);
    }];
    
    UIView *bottomLine_code = [[UIView alloc]init];
    bottomLine_code.backgroundColor = KLColor_B3;
    [phoneNumView_new addSubview:bottomLine_code];
    [bottomLine_code mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(phoneNumView_new);
        make.width.mas_equalTo(325*WIDTH);
        make.height.mas_equalTo(1);
    }];
    // 验证码
    
    UIView *pwdView = [[UIView alloc]init];
    pwdView.backgroundColor = [UIColor whiteColor];
    [self addSubview:pwdView];
    [pwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumView_new.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(60*HEIGHT);
    }];
    
    UIImageView *pwdImgView = [[UIImageView alloc]init];
    pwdImgView.image = [UIImage imageNamed:@"login_icon_yanzhengma"];
    pwdImgView.contentMode = UIViewContentModeScaleAspectFit;
    [pwdView addSubview:pwdImgView];
    [pwdImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(pwdView).offset(30*HEIGHT);
        make.left.equalTo(pwdView).offset(25*WIDTH);
    }];
    
    UITextField *pwdTF = [[UITextField alloc]init];
    pwdTF.placeholder = @"请输入验证码";
    pwdTF.secureTextEntry = YES;
    pwdTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    pwdTF.textColor = KLColor_B2;
    pwdTF.textAlignment = NSTextAlignmentLeft;
    [pwdView addSubview:pwdTF];
    [[pwdTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(UITextField *code) {
        self.codeStr = code.text;
    }];
    [pwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pwdImgView);
        make.width.mas_equalTo(200*WIDTH);
        make.left.equalTo(pwdImgView.mas_right).offset(18*WIDTH);
    }];
    
    
    UIView *bottomLine_pwd = [[UIView alloc]init];
    bottomLine_pwd.backgroundColor = KLColor_B3;
    [pwdView addSubview:bottomLine_pwd];
    [bottomLine_pwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(pwdView);
        make.width.mas_equalTo(325*WIDTH);
        make.height.mas_equalTo(1);
    }];
    
    // 保存按钮
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
    [saveBtn setBackgroundImage:[UIImage createImageWithColor:KLColor_A1] forState:UIControlStateNormal];
    [self addSubview:saveBtn];
    self.saveBtn = saveBtn;
    RAC(saveBtn,enabled) = [RACSignal combineLatest:@[RACObserve(self, phoneNumStr_new),RACObserve(self, phoneNumStr_old),RACObserve(self, codeStr)] reduce:^id(NSString *phoneStr_new,NSString*codeStr,NSString*pwdStr_old){
        BOOL enable;
        enable = phoneStr_new.length>0&&codeStr.length>0&&pwdStr_old.length>0;
        return @(enable);
    }];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(pwdView.mas_bottom).offset(36*HEIGHT);
        make.width.mas_equalTo(345*WIDTH);
        make.height.mas_equalTo(44*HEIGHT);
    }];
    
    
}


// 圆角
- (void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    [self.saveBtn layoutIfNeeded];
    [self.saveBtn setRadiusWithValue:22*HEIGHT];
}
@end
